#include "collapseviewitem.h"
#include "ui_collapseviewitem.h"

#include "litteriem.h"

#include <QDebug>
#include <QTime>


#include "chatform.h"



int MAX_PERSON_PIC_NUM=9;

CollapseViewItem::CollapseViewItem(QString titleText, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CollapseViewItem)
{
    m_parent=this;
    ui->setupUi(this);
    ui->label->setProperty("slected",!ui->item_con->isVisible());

    mItems.clear();
/*
    //根据时间制作随机种子
    QTime time;
    time= QTime::currentTime();
    qsrand(time.msec()+time.second()*1000);


    //好友数目
    int personNumber=qrand()%(MAX_PERSON_PIC_NUM-1)+1;

    for(int i=0;i<personNumber;i++){
        //随机选择好友头像

        QString picPath=QString(":/media/person/media/person/%1.jpg")
                .arg(QString::number(qrand()%(MAX_PERSON_PIC_NUM-1)+1));
        addSubItem(new LitterIem(picPath,this));

    }


    qDebug()<<"personNumber:"<<personNumber;

    //随机产生未上线好友数目
    int offlinPerson=qrand()%personNumber;

    titleText=QString("%1 (%2/%3)")
            .arg(titleText,QString::number(offlinPerson),QString::number(personNumber));

*/
    this->setTitleText(titleText);
    m_titleText=titleText;
    //qDebug()<<"线程主"<<pthread_self();

    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(onCollapseButClick()));
    //connect(ui->listWidget,SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(onChat(QListWidgetItem*)));//&LitterIem::onTalkWindowItemClicked);
}

CollapseViewItem::~CollapseViewItem()
{
    foreach (LitterIem *var, mItems) {
        delete var;
    }
    mItems.clear();
    delete ui;
}

void CollapseViewItem::setTitleText(QString titleText)
{
    ui->pushButton->setText(titleText);
}
LitterIem* CollapseViewItem::addItem(int fd,QString uuid,QString text,QString mood)
{
    //qDebug()<<text;
     //   qDebug()<<"线程ID"<<pthread_self();
    //随机选择好友头像
    QString picPath=QString(":/media/person/media/person/%1.jpg")
            .arg(QString::number(qrand()%(MAX_PERSON_PIC_NUM-1)+1));

    LitterIem *it=new LitterIem(picPath,this);
    it->setObjectName(uuid);
    it->displayNickname(text);
    it->displayMood(mood);
    it->setUUID(uuid);
    it->setFd(fd);
    addSubItem(it);
    //qDebug()<<it->objectName()<<"\tuuid:"<<uuid;
    mItems.append(it);    
    int offlinPerson=mItems.size();
    int personNumber=mItems.size();
    QString titleText=QString("%1 (%2/%3)")
            .arg(m_titleText,QString::number(offlinPerson),QString::number(personNumber));
    this->setTitleText(titleText);
    return it;
}
void CollapseViewItem::delItem(QString uuid)
{
    /*
    int i=0;
    foreach (LitterIem *var, mItems) {
        if(var->isUUID(uuid)){
            //qDebug()<<var->objectName();
            //QList<QListWidgetItem*> list= ui->listWidget->findItems(var->objectName(),Qt::MatchCaseSensitive);
            //if(list.size()>0)
            //ui->listWidget->removeItemWidget(list[0]);
            var->setParent(NULL);
            delete var;
            mItems.removeAt(i);
            ui->listWidget->takeItem(i);

            break;
        }
        else
            i++;
    }
    int offlinPerson=mItems.size();
    int personNumber=mItems.size();
    QString titleText=QString("%1 (%2/%3)")
            .arg(m_titleText,QString::number(offlinPerson),QString::number(personNumber));
    this->setTitleText(titleText);
    */
}
void CollapseViewItem::addSubItems(QList<QWidget *> widgets)
{
    foreach (QWidget *var, widgets) {
        addSubItem(var);
    }
}

void CollapseViewItem::addSubItem(QWidget *widget)
{
    //ui->item_con->layout()->addWidget(widget);
/*
    QListWidgetItem *aItem = new QListWidgetItem(ui->listWidget);
        aItem->setSelected(true);
        ui->listWidget->addItem(aItem);        
        aItem->setSizeHint(widget->size());
        ui->listWidget->setItemWidget(aItem, widget);
*/
}

void CollapseViewItem::onCollapseButClick()
{
    //设置为相反的显示状态
    ui->item_con->setVisible(!ui->item_con->isVisible());

    ui->label->setProperty("slected",ui->item_con->isVisible());

    ui->label->update();

}
void CollapseViewItem::onChat(QListWidgetItem* item)
{
    //qDebug()<<"onchat()"<<item;
    //QWidget *pwidget=ui->listWidget->itemWidget(item);
    //qDebug()<<pwidget;
    //qDebug()<<m_parent;
    //qDebug()<<pwidget->objectName();
    //((CollapseView*)m_parent)->setTalkwnd(pwidget->objectName());
    //emit sigTalkWindow(pwidget->objectName());
}
